package com.huazi.www.drug.drugy.dao.impl;


import com.huazi.www.drug.drugy.dao.DoctorDao;
import com.huazi.www.drug.drugy.entity.Doctor_information;
import com.huazi.www.drug.drugy.util.BaseDAO;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 波——最帅的男人
 * @version 1.0
 * @date 2021/5/13 16:05
 */
public class DoctorDaoImpl extends BaseDAO implements DoctorDao {
    /**
     * 医生查询分页
     *
     * @param name
     * @param start
     * @param end
     * @return
     */

    public List<Doctor_information> selectDoctor(String name, Integer start, Integer end) {
        List<Doctor_information> list = new ArrayList<>();
        String sql = "select * from doctor_information where 1=1 ";
        if (!name.equals("")) {
            sql += " and doctorname like concat('%',?,'%') limit ?,?";
            rs = executeQuery(sql, name, start, end);
        } else {
            sql += " limit ?,?";
            rs = executeQuery(sql, start, end);
        }
//        System.out.println(sql+" "+name);
        try {
            Doctor_information d = null;
            while (rs.next()) {
                int anInt = rs.getInt(1);
                String string = rs.getString(2);
                String string1 = rs.getString(3);
                int anInt1 = rs.getInt(4);
                String string2 = rs.getString(5);
                String string3 = rs.getString(6);
                String string4 = rs.getString(7);
                String string5 = rs.getString(8);
                String string6 = rs.getString(9);
                String string7 = rs.getString(10);
                Date date = rs.getDate(11);
                d = new Doctor_information(anInt, string, string1, anInt1, string2, string3, string4, string5, string6, string7, date);
                list.add(d);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Close(rs, ps, conn);
        }
        return list;
    }

    /**
     * 查询医生数量
     *
     * @param name
     * @return
     */

    public int selectDoctorCount(String name) {
        int count = 0;
        String sql = "select count(1) from doctor_information where 1=1 ";
        if (!name.equals("")) {
            sql += "and doctorname like concat('%',?,'%')";
            rs = executeQuery(sql, name);
        } else {
            rs = executeQuery(sql);
        }
        try {
            rs.next();
            count = rs.getInt(1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Close(rs, ps, conn);
        }
        return count;
    }

    /**
     * 新增医生
     *
     * @param doctor
     * @return
     */

    public int insertDoctor(Doctor_information doctor) {
        String sql = "INSERT INTO doctor_information(`doctorname`, `doctorSex`, `doctorAge`, `doctorke`, `doctorcard`, `doctorphone`, `position`, `type`, `doctorType`, `doctorDate`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        return executeUpdate(sql, doctor.getDoctorname(), doctor.getDoctorSex(), doctor.getDoctorAge(), doctor.getDoctorke(), doctor.getDoctorcard(), doctor.getDoctorphone(), doctor.getPosition(), doctor.getType(), doctor.getDoctorType(), doctor.getDoctorDate());
    }

    /**
     * 删除医生
     *
     * @param id
     * @return
     */

    public int deleteDoctor(Integer id) {
        String sql = "delete from doctor_information where doctorId=?";
        return executeUpdate(sql, id);
    }

    /**
     * 更新医生信息
     *
     * @param
     * @return
     */


    public int updateDoctor(Doctor_information doctor) {
        String sql = "update doctor_information set doctorname=?,doctorSex=?,doctorAge=?,doctorke=?,doctorcard=?,doctorphone=?,position=?,type=?,doctorType=?,doctorDate=? where doctorId=?";
        return executeUpdate(sql, doctor.getDoctorname(), doctor.getDoctorSex(), doctor.getDoctorAge(), doctor.getDoctorke(), doctor.getDoctorcard(), doctor.getDoctorphone(), doctor.getPosition(), doctor.getType(), doctor.getDoctorType(), doctor.getDoctorDate(), doctor.getDoctorId());
    }
}
